package cn.aeolia.controller;

import cn.aeolia.dao.AccountDao;
import cn.aeolia.pojo.Account;
import cn.aeolia.utils.I18nUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 登录验证
 *
 * @author aeolia
 */
@Controller
public class LoginController {
    @Autowired
    private AccountDao accountDao;

    /**
     * 登录验证
     *
     * @param modelAndView
     * @param account
     * @param session
     * @return
     */
    @RequestMapping("/loginCheck")
    public ModelAndView longinCheck(ModelAndView modelAndView, Account account, HttpSession session, HttpServletRequest request) {
        if (account.getUsername() == null || account.getUsername() == "") {
            //账号为空判断
            modelAndView.addObject("msg", I18nUtils.getValue("login.msg_noAccount"));
            modelAndView.setViewName("login");
        } else if (account.getPassword() == null || account.getPassword() == "") {
            //密码为空判断
            modelAndView.addObject("msg", I18nUtils.getValue("login.msg_noPassword"));
            modelAndView.setViewName("login");
        } else {
            //访问数据库根据提供的账号密码拿到账户数据(模拟的数据,没有连接实际数据库只是查询存放账户信息的集合)
            Account accountByUP = accountDao.getAccountByUP(account);
            if (null != accountByUP) {
                //如果不为空说明账号密码与数据库中一致，通过登录，并将用户放入session中
                session.setAttribute("loginUser", accountByUP);
                modelAndView.setViewName("redirect:index");
            } else {
                //如果为空说明账号密码不一致
                modelAndView.addObject("msg", I18nUtils.getValue("login.msg_errorAccountOrPassword"));
                modelAndView.setViewName("login");
            }
        }
        return modelAndView;
    }
}
